<?php
/**
 * Samble LDAP class
 * @author Lim afriyadi
 * @license GNU/GPL v.3
 * @version pre-alpha
 * @package Samble
 * @subpackage ldap
 */
class LDAP
{
  /**
   * The class singleton instance.
   */
  private static $oInstance;
  /**
   * LDAP server address/name.
   * @var string $sHost
   */
  var $sHost = "";
  /**
   * LDAP port number.
   * @var int
   */
  var $iPort = "";
  /**
   * LDAP basedn.
   * @var string
   */
  var $sBasedn = "";
  /**
   * LDAP binddn.
   * @var string
   */
  var $sBinddn = "";
  /**
   * LDAP bind password.
   * @var <type>
   */
  var $sBindpw = "";
  /**
   * LDAP search filter.
   * @var string
   */
  var $sFilter = "";
  /**
   * Bind connection object.
   * @var object
   */
  var $oBind = null;
  /**
   * LDAP connection object.
   * @var object
   */
  var $oConnection = null;

  /**
   *
   */
  private function __CONSTRUCT() { }

  /**
   * Return this class single instance.
   * @return object
   */
  public function getInstance()
  {
    if(!self::$oInstance)
    {
      self::$oInstance = new LDAP();
    }
    return self::$oInstance;
  }

  /**
   * Set sHost class variable.
   * @param string $sHost
   * @return boolean
   */
  public function setHost($sHost="")
  {
    if($sHost != "")
    {
      $this->sHost = $sHost;
      return true;
    }
    return false;
  }

  /**
   * Get sHost class variable.
   * @return string
   */
  public function getHost()
  {
    return $this->sHost;
  }

  /**
   * Set iPort class variable.
   * @param int $iPort
   * @return boolean
   */
  public function setPort($iPort=0)
  {
    if($iPort!=0 && is_numeric($iPort))
    {
      $this->iPort = $iPort;
      return true;
    }
    return false;
  }

  /**
   * Get iPort class variable.
   * @return int
   */
  public function getPort()
  {
    return $this->iPort;
  }

  /**
   * Set sBasedn class variable.
   * @param string $sBasedn
   * @return boolean
   */
  public function setBasedn($sBasedn="")
  {
    if($sBasedn != "")
    {
      $this->sBasedn = $sBasedn;
      return true;
    }
    return false;
  }

  /**
   * Get sBasedn class variable.
   * @return string
   */
  public function getBasedn()
  {
    return $this->sBasedn;
  }

  /**
   * Set sBinddn class variable.
   * @param string $sBinddn
   * @return boolean
   */
  public function setBinddn($sBinddn="")
  {
    if($sBinddn != "")
    {
      $this->sBinddn = $sBinddn;
      return true;
    }
    return false;
  }

  /**
   * Get sBinddn class variable.
   * @return string
   */
  public function getBinddn()
  {
    return $this->sBinddn;
  }

  /**
   * Set sBindpw class variable.
   * @param string $sBindpw
   * @return boolean
   */
  public function setBindpw($sBindpw="")
  {
    if($sBindpw != "")
    {
      $this->sBindpw = $sBindpw;
      return true;
    }
    return false;
  }

  /**
   * Get sBindpw class variable.
   * @return string
   */
  public function getBindpw()
  {
    return $this->sBindpw;
  }

  /**
   * Set sFilter class variable.
   * @param string $sFilter
   * @return boolean
   */
  public function setFilter($sFilter="")
  {
    if($sFilter != "")
    {
      $this->sFilter = $sFilter;
      return true;
    }
    return false;
  }

  /**
   * Get sFilter class variable.
   * @return string
   */
  public function getFilter()
  {
    return $this->sFilter;
  }

  /**
   * Clear sBinddn, sBindpw, and sFilter class variable.
   */
  public function clearBind()
  {
    $this->sBinddn = "";
    $this->sBindpw = "";
    $this->sFilter = "";
  }

  /**
   * LDAP connect function.
   * @param string $sHost
   * @param int $iPort
   * @param string $sBinddn
   * @param string $sBindpw
   * @return boolean
   */
  public function ldapConnect($sHost="",$iPort=389,$sBinddn="",$sBindpw="")
  {
    if( $this->setHost($sHost) && $this->setPort($iPort) )
    {
      $this->setBinddn($sBinddn);
      $this->setBindpw($sBindpw);
      $this->oConnection = ldap_connect($this->getHost(),$this->getPort());
      ldap_set_option($this->oConnection,LDAP_OPT_PROTOCOL_VERSION,3);
      if(ldap_bind($this->oConnection,$this->getBinddn(),$this->getBindpw())) return true;
    }
    return false;
  }

  /**
   * Closes LDAP connection.
   * @return boolean
   */
  public function ldapClose()
  {
    if($this->oConnection != null)
    {
      ldap_close($this->oConnection);
      $this->oConnection = null;
      return true;
    }
    return false;
  }

  /**
   * LDAP search function. Depend on LDAP connection and bind.
   * @param string $sBasedn
   * @param string $sFilter
   * @param string $aAttributes
   * @return boolean
   */
  public function ldapSearch($sBasedn,$sFilter,$aAttributes="")
  {
    if($this->oConnection != null)
    {
      if($this->setBasedn($sBasedn) && $this->setFilter($sFilter))
      {
	if(is_array($aAttributes))
	{
	  $search = ldap_search($this->oConnection,$this->getBasedn(),$this->getFilter(),$aAttributes);
	}
	else
	{
	  $search = ldap_search($this->oConnection,$this->getBasedn(),$this->getFilter());
	}
	$result = ldap_get_entries($this->oConnection,$search);
	return $result;
      }
    }
    return false;
  }

  /**
   * Return LDAP attribute defined from LDAP search action.
   * @param object $oLdapSearch
   * @param string $sAttribute
   * @return boolean
   */
  public function getAttributeValue($oLdapSearch=false,$sAttribute="")
  {
    if($sAttribute != "" && $oLdapSearch != false)
    {
      $results = $oLdapSearch;
      $output = array();
      $i = 0;
      foreach($results as $result)
      {
	if(is_array($result))
	{
	  foreach($result as $key=>$val)
	  {
	    if(is_array($val) && $key == $sAttribute)
	    {
	      $output[$i] = $val[0];
	      $i++;
	    }
	  }
	}
      }
      return $output;
    }
    return false;
  }

  /**
   * Get oConnection class variable.
   * @return false || oConnection class variable
   */
  public function getConnection()
  {
    if($this->oConnection != null)
    {
      return $this->oConnection;
    }
    return false;
  }
}
?> 
